<?php

namespace app\reserve\controller;
use think\Controller;
use think\Db;
use think\Request;
use app\reserve\model\Bankcard as BankcardModel;

class Bankcard extends Controller{
    /*
    * Notes:
    * Create Date: 2018/10/31
    * Create Time: 下午1:55
    * Create by :张福来
    * a_id :  商户id
    * number :  银行卡号
    * openbank :  开户行
    * name :  联系人
    * phone :  联系电话
    * ctime :  创建时间
    * utime :  修改时间  
    */
    public function addBankcard(Request $request){
        $bank = new BankcardModel();
        $postData = $request->post();
        $check = $this->validate($postData,"BankcardValidate.addBankcard");
        if($check !== true){
            return apiResponse(201,$check,"");
        }
        $default = $bank->selectBankcard(1,10,["mix_id"=>$postData["mix_id"],"type"=>$postData["type"]]);
        $count = count($default["list"]);
        if(!$count > 0){
            $postData["defaultcard"] = 0;
        }else{
            $postData["defaultcard"] = 1;
        }
        $result = $bank->addBankcard($postData);
        if($result){
            return apiResponse(200,"添加银行卡成功",$result);
        }else{
            return apiResponse(201,"添加银行卡失败",$result);
        }
    }
    /*
    * Notes: 银行卡列表
    * Create Date: 2018/10/31
    * Create Time: 下午3:31
    * Create by :张福来
    * param :
    */
    public function selectBankcard(Request $request){
        $bank = new BankcardModel();
        $postData = $request->post();
        $check = $this->validate($postData,"BankcardValidate.selectBankcard");
        $pagenum = $postData['pagenum'];
        if($check !== true){
            return apiResponse(201,$check,"");
        }
        if(!empty($postData["listNum"])){
            $listNum = $postData["listNum"];
        }else{
            $listNum = 10;
        }
        if(!empty($postData["mix_id"])){
            $w["mix_id"] = $postData["mix_id"];
        }
        if(!empty($postData["type"])){
            $w["type"] = $postData["type"];
        }
        $w["status"] = array('NEQ',9);
        if(!empty($postData["name"])){
            $w['name'] = array('LIKE','%'.trim($postData["name"]).'%');
        }
        if(!empty($postData["openbank"])){
            $w['openbank'] = array('LIKE','%'.trim($postData["openbank"]).'%');
        }
        if(!empty($postData["number"])){
            $w['number'] = $postData["number"];
        }
        if(!empty($postData["phone"])){
            $w['phone'] = $postData["phone"];
        }
        if(!empty($postData["status"])){
            $w["status"] = $postData["status"];
        }
        if (!empty($postData['defaultcard'])){
            $w["defaultcard"] = $postData["defaultcard"];
        }
        if(!empty($postData["start_time"]) && !empty($postData["end_time"])){
            $start_time = $postData["start_time"] ." 00:00:00";
            $end_time = $postData["end_time"] . " 23:59:59";
            $w['ctime'] = array(array('EGT',strtotime($start_time)),array('ELT',strtotime($end_time)),'and');
        }
        $result = $bank -> selectBankcard($pagenum,$listNum,$w);
        if(empty($result["list"])){
            return apiResponse(200,"当前无数据",$result);
        }else{
            $count = count($result["list"]);
            for($i=0;$i<$count;$i++){
                if(empty($result["list"][$i]["pic"])){
                    $result["list"][$i]["pic"] = "/upload/bankcard/card.png";
                }
            }
            return apiResponse(200,"查询成功",$result);
        }
    }
    /*
    * Notes: 修改银行卡信息
    * Create Date: 2018/10/31
    * Create Time: 下午5:21
    * Create by :张福来
    * param :
    */
    public function eidtBankcard(Request $request){
        $bank = new BankcardModel();
        $postData = $request->post();
        $check = $this->validate($postData,"BankcardValidate.eidtBankcard");
        if($check !== true){
            return apiResponse(201,$check,"");
        }
        $postData['utime'] = time();
        $result = $bank->eidtBankcard($postData);
        if($result){
            return apiResponse(200,"修改银行卡成功",$result);
        }else{
            return apiResponse(201,"修改银行卡失败",$result);
        }
    }
    /*
    * Notes:
    * Create Date: 2018/10/31 
    * Create Time: 下午6:41
    * Create by :张福来
    * param : 
    */
    public function deleteBankcard(Request $request){
        $bank = new BankcardModel();
        $postData = $request->post();
        $check = $this->validate($postData,"BankcardValidate.deleteBankcard");
        if($check !== true){
            return apiResponse(201,$check,"");
        }
        $w["id"] = $postData["id"];
        $result = $bank->deleteBankcard($w);
        if($result){
            return apiResponse(200,"删除银行卡成功",$result);
        }else{
            return apiResponse(201,"删除银行卡失败",$result);
        }
    }

    /**
     * Notes:  修改默认值
     * Create Date: 2018/10/9
     * Create by :lijie
     * param：mix_id
     * param：type
     * param：id：银行卡id
     */
    public function editDefault(Request $request)
    {
        $bank = new BankcardModel();
        $param = $request->post();
        // 开启事务
        Db::startTrans();
        // 全部改为非默认
        $where['mix_id']    = $param['mix_id'];
        $where['type']      = $param['type'];
        $where['status']    = array("NEQ",9);
        $data['defaultcard'] = 1;
        $res = $bank->isUpdate(true)->allowField(true)->save($data,$where);
        // 修改默认
        $dataDefau['defaultcard'] = 0;
        $whereDefault['id']       = $param['id'];
        $whereDefault['status']   = array("NEQ",9);
        $resDefau = $bank->isUpdate(true)->allowField(true)->save($dataDefau,$whereDefault);
        //
        if ($res && $resDefau){
            Db::commit();
            return apiResponse(200,"修改成功",'');
        }else {
            Db::rollback();
            return apiResponse(201,"修改失败",'');
        }
    }
}